<?php

namespace App\Http\Controllers\Buy;

use App\Alipay\Alipay;
use App\Models\Order;
use App\Models\OrderAction;
use App\Models\Trade;
use App\Models\Wallet;

class PayController extends BaseController
{
    public function index()
    {
        $step = 'payOrder';
        $order_id = $this->request->input('order_id');
        $order = Order::where('order_id', $order_id)->first();

        if ($order->pay_status) {
            return redirect('/buy/pay/success?order_id='.$order_id);
        }

        $wallet = Wallet::getData($this->uid);
        $this->assign(compact('order_id', 'order', 'wallet', 'step'));

        return $this->view('buy.pay');
    }

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|string
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function alipay()
    {
        $order_id = $this->request->input('order_id');
        $order_no = $this->request->input('order_no');
        $order = $trade = [];

        if ($order_id) {
            $order = Order::where('order_id', $order_id)->first();
        }elseif ($_GET['order_no']) {
            $order = Order::where('order_no', $order_no)->first();
        }

        //已支付订单直接跳转
        if ($order->pay_status == 1){
            return redirect('/buy/order_query?order_id='.$order_id);
        }

        $trade = Trade::where('trade_no', $order->trade_no)->first();
        return Alipay::pay([
            'return_url'=>url('/buy/alipay_query')
        ])->webPay([
            'out_trade_no'=>$trade->out_trade_no,
            'total_amount'=>$trade->total_amount,
            'subject'=>$trade->subject,
        ]);
    }

    /**
     * @return mixed
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function alipayQuery()
    {
        $out_trade_no = $this->request->input('out_trade_no');

        $query = Alipay::pay()->query(compact('out_trade_no'));

        if ($query->tradeSuccess()) {
            $order = Order::where('order_no', $out_trade_no)->first();
            if (!$order->pay_status) {
                Trade::where('out_trade_no', $out_trade_no)->update([
                    'pay_type'=>'alipay',
                    'pay_status'=>1,
                    'pay_at'=>time(),
                    'updated_at'=>time(),
                    'trade_status'=>2
                ]);

                Order::where('order_no', $out_trade_no)->update([
                    'pay_status'=>1,
                    'pay_at'=>time(),
                    'updated_at'=>time(),
                    'trade_status'=>2
                ]);

                OrderAction::insert([
                    'order_id'=>$order->order_id,
                    'uid'=>$this->uid,
                    'username'=>$this->username,
                    'content'=>trans('mall.pay success'),
                    'created_at'=>time()
                ]);
            }

            return redirect('/buy/pay/success?order_id='.$order->order_id);
        } else {
            return $this->showError($query->subMsg());
        }
    }

    public function success()
    {

        $step = 'paySuccess';
        $order_id = $this->request->input('order_id');
        $order = Order::where('order_id', $order_id)->first();
        $this->assign(compact('order_id', 'order', 'step'));

        return $this->view('buy.success');
    }
}
